package com.tdcy.biz.wxmgt.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.result.WxMpUserList;
import me.chanjar.weixin.mp.bean.tag.WxUserTag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tdcy.biz.wxmgt.service.WeixinService;
import com.tdcy.biz.wxmgt.service.WxMgtService;
import com.tdcy.biz.wxmgt.service.bean.FansBean;
import com.tdcy.biz.wxmgt.service.bean.UserTagBean;
import com.tdcy.framework.BaseController;
import com.tdcy.framework.bean.PageInfo;
import com.tdcy.framework.util.RequestUtils;
import com.tdcy.framework.util.WebUtils;

/**
*/
@Controller
@RequestMapping(value = "/wxmgt")
public class WxMgtController extends BaseController {
	@Autowired
	WxMgtService wxMgtSvc;

	@Autowired
	WeixinService wxMpService;

	@ResponseBody
	@RequestMapping(value = "/saveWxAcct", method = RequestMethod.POST)
	public Object saveWxAcct() throws IOException {
		Map<String, String> paramMap = RequestUtils.getAllParameters(this
				.getRequest());
		wxMgtSvc.saveWxParam(paramMap);
		
		wxMpService.init();
		return WebUtils.createJSONSuccess("更新成功");
	}

	@ResponseBody
	@RequestMapping(value = "/getFans", method = RequestMethod.POST)
	public Object getFans(FansBean fansBean) throws IOException,
			WxErrorException {
		Object nextOpenIdObj = getRequest().getSession().getAttribute("nextOpenId");
		String nextOpenId = "";
		if(nextOpenIdObj!=null){
			nextOpenId = (String)nextOpenIdObj;
		}
		WxMpUserList wxUserList = wxMpService.getUserService().userList(
				nextOpenId);

		PageInfo<WxMpUser> fansPage = new PageInfo<WxMpUser>();
		long total = wxUserList.getTotal();
		fansPage.setTotalRecord((int) total);
		int page = 1;
		if (total % 1000 == 0) {
			page = (int) total / 1000;
		} else {
			page = (int) total / 1000 + 1;
		}
		fansPage.setTotalPage(page);
		long count = wxUserList.getCount();
		getRequest().getSession().setAttribute("nextOpenId", nextOpenId);
		List<WxMpUser> userList = new ArrayList<WxMpUser>();
		List<String> openIds = wxUserList.getOpenids();

//		List<String> tids = new ArrayList<String>();
//		for (int i = 1; i <= openIds.size(); i++) {
//			if (i % 100 == 0) {
//				List<WxMpUser> tList = wxMpService.getUserService().userInfoList(tids);
//				if(tList!=null){
//					userList.addAll(tList);
//				}
//				tids = new ArrayList<String>();
//			} else {
//				tids.add(openIds.get(i-1));
//			}
//		}
//		if (tids.size() > 0) {
//			List<WxMpUser> tList = wxMpService.getUserService().userInfoList(tids);
//			if(tList!=null){
//				userList.addAll(tList);
//			}
//		}
		
		for (int i = 1; i <= openIds.size(); i++) {
			WxMpUser user = wxMpService.getUserService().userInfo(openIds.get(i-1));
			userList.add(user);
		}
		//System.out.println(tids);
		//System.out.println(userList.size());
		
		fansPage.setResultsList(userList);
		fansPage.setPageSize(1000);
		fansPage.setCurPageNO(fansBean.getPage());
		return WebUtils.createJSONSuccess("更新成功", fansPage);
	}
	
	@ResponseBody
	@RequestMapping(value = "/getUserTags", method = RequestMethod.POST)
	public Object getUserTags(UserTagBean userTagBean) throws IOException,
			WxErrorException {
		List<WxUserTag> res = wxMpService.getUserTagService().tagGet();
		return WebUtils.createJSONSuccess("更新成功", res);
	}
}
